package com.shishuo.cms.action.manage;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.shishuo.cms.entity.Authorization;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.entity.vo.PageVo;
import com.shishuo.cms.service.AuthorizationService;
import com.shishuo.cms.util.MediaUtils;

@RequestMapping("/manage/authorization")
@Controller
public class ManageAuthorizationAction extends ManageBaseAction {

	@Autowired
	private AuthorizationService authorizationService;

	@RequestMapping(value = "/list.htm", method = RequestMethod.GET)
	public String list(ModelMap modelMap,
			@RequestParam(value = "p", defaultValue = "1") int p,
			@RequestParam(value = "weixin", required = false) String weixin,
			@RequestParam(value = "name", required = false) String name
			)
			throws Exception {
		PageVo<Authorization> pageVo = authorizationService.getAuthorizationPage(p,weixin,name);
		modelMap.put("pageVo", pageVo);
		modelMap.put("name", name);
		modelMap.put("weixin", weixin);
		return "manage/authorization/list";
	}
	
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String details( 
			ModelMap modelMap) throws Exception {
		return "manage/authorization/add";
	}
	
	@ResponseBody
	@RequestMapping(value = "/add.json", method = RequestMethod.POST)
	public JsonVo<String> addReply(@RequestParam(value = "name") String name,
			@RequestParam(value = "weixin") String weixin,
			@RequestParam(value = "file", required = false) MultipartFile file,
			ModelMap modelMap) {
		JsonVo<String> json = new JsonVo<String>();
		if (StringUtils.isBlank(name)) {
			json.getErrors().put("name", "名称不能为空");
			json.setMsg("名称不能为空");
		}
		if (StringUtils.isBlank(weixin)) {
			json.getErrors().put("weixin", "微信号不能为空");
			json.setMsg("微信号不能为空");
		}
		if (file==null) {
			json.getErrors().put("file", "授权书不能为空");
			json.setMsg("授权书不能为空");
		}else{
			if (!MediaUtils.isImageType(file.getOriginalFilename())) {
				json.getErrors().put("file", "不是图片类型");
				json.setMsg("不是图片类型");
			}
		}
		try {
			// 检测校验结果
			Authorization authorization = authorizationService.selectByCondition(weixin);
			if (authorization!=null) {
				json.getErrors().put("weixin", "该微信号已存在");
				json.setMsg("该微信号已存在");
			} 
			json.check();
			if (file != null && !file.isEmpty()) {
				String url = MediaUtils.saveImage(file, 0,0);
				authorizationService.addGuestbook(name, weixin, url);
			}
			json.setResult(true);
		} catch (Exception e) {
			json.setResult(false);
		}
		return json;
	}

	@RequestMapping(value = "/details.htm", method = RequestMethod.GET)
	public String details(@RequestParam("authorizationId") long authorizationId,
			ModelMap modelMap) throws Exception {
		Authorization authorization = authorizationService.getAuthorizationById(authorizationId);
		modelMap.put("authorization", authorization);
		return "manage/authorization/update";
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.POST)
	public JsonVo<String> deleteFile(
			@RequestParam(value = "authorizationId") long authorizationId) {
		JsonVo<String> json = new JsonVo<String>();
		authorizationService.deleteAuthorization(authorizationId);
		json.setResult(true);
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> status(
			@RequestParam(value = "name") String name,
			@RequestParam(value = "authorizationId") long authorizationId,
			@RequestParam(value = "weixin") String weixin,
			@RequestParam(value = "file", required = false) MultipartFile file,
			ModelMap modelMap) {
		JsonVo<String> json = new JsonVo<String>();
		if (StringUtils.isBlank(name)) {
			json.getErrors().put("name", "名称不能为空");
			json.setMsg("名称不能为空");
		}
		if (StringUtils.isBlank(weixin)) {
			json.getErrors().put("weixin", "微信号不能为空");
			json.setMsg("微信号不能为空");
		}
		if(file!=null){
			if (!MediaUtils.isImageType(file.getOriginalFilename())) {
				json.getErrors().put("file", "不是图片类型");
				json.setMsg("不是图片类型");
			}
		}
		try {
			// 检测校验结果
			json.check();
			String url=null;
			if (file != null && !file.isEmpty()) {
				url = MediaUtils.saveImage(file, 0,0);
			}
			Authorization authorization=new Authorization();
			authorization.setUrl(url);
			authorization.setName(name);
			authorization.setAuthorizationId(authorizationId);
			authorization.setWeixin(weixin);
			authorizationService.updateAuthorization(authorization);
			json.setResult(true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.setResult(false);
			json.setMsg(e.getMessage());
		}
		return json;
	} 

}
